package com.wyg.state;

/**
 * @description:    状态模式
 * @author: WYG
 * @time: 2021/4/7 8:56
 */

public class OldGumballMachine {
    /**
     * 糖果售罄
     */
    private final static int SOLD_OUT = 0;
    /**
     * 糖果售卖机没有钱
     */
    private final static int NO_QUARTER = 1;
    /**
     * 糖果售卖机 已投入钱
     */
    private final static int HAS_QUARTER = 2;
    /**
     * 售出
     */
    private final static int SOLD = 3;

    private int state = SOLD;
    private int count = 0;

    public OldGumballMachine(int count) {
        this.count = count;
        if (count > 0) {
            state = NO_QUARTER;
        }
    }

    public void insertQuarter() {
        if (state == HAS_QUARTER) {
            System.out.println("您已经投入了5元钱");
        } else if (state == NO_QUARTER) {
            System.out.println("您还没有投钱");
        } else if (state == SOLD_OUT) {
            System.out.println("糖果已经卖光了");
        } else if (state == SOLD) {
            System.out.println("请稍等，我们正在为您出糖果");
        }
    }

    public void ejectQuarter() {
        if (state == HAS_QUARTER) {
            System.out.println("您已投入5元钱，无须再投钱");
            state = NO_QUARTER;
        } else if (state == NO_QUARTER) {
            System.out.println("您还没有投钱");
        } else if (state == SOLD) {
            System.out.println("不好意思，您已经转动了曲柄，无法再退钱");
        } else if (state == SOLD_OUT) {
            System.out.println("抱歉，您没有投入钱");
        }
    }

    public void turnCrank() {
        if (state == SOLD) {
            System.out.println("别想骗机器拿两次糖果");
        } else if (state == NO_QUARTER) {
            System.out.println("您需要先投钱");
        } else if (state == SOLD_OUT) {
            System.out.println("不能给您糖果了，糖果已经售罄了");
        } else if (state == HAS_QUARTER) {
            System.out.println("正在出糖果");
            state = SOLD;
            dispense();
        }
    }
    public void dispense() {
        if (state == SOLD) {
            System.out.println("我们正在售出糖果状态");
            count = count - 1;
            if (count == 0) {
                System.out.println("糖果已经售罄了");
                state = SOLD_OUT;
            } else {
                // 如果糖果没有售罄，那就继续将状态转换到没有投递钱的状态
                state = NO_QUARTER;
            }
        } else if (state == NO_QUARTER) {
            System.out.println("您需要先投钱");
        } else if (state == SOLD_OUT) {
            System.out.println("无法分配");
        } else if (state == HAS_QUARTER) {
            System.out.println("无法分配");
        }
    }

    @Override
    public String toString() {
        return "GumballMachine{" +
                "state=" + state +
                ", count=" + count +
                '}';
    }
}
